// ClassStudentService.java
package com.example.teacherservice.service;

import com.example.teacherservice.entity.StuClass;
import com.example.teacherservice.repository.StuClassRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClassStudentService {

    @Autowired
    private StuClassRepository stuClassRepository;

    @Transactional
    public void addStudentToClass(Long classId, Long studentId) {
        if (!stuClassRepository.existsByStuIdAndClassId(studentId, classId)) {
            StuClass stuClass = new StuClass();
            stuClass.setClassId(classId);
            stuClass.setStuId(studentId);
            stuClassRepository.save(stuClass);
        }
    }

    @Transactional
    public void removeStudentFromClass(Long classId, Long studentId) {
        stuClassRepository.deleteByStuIdAndClassId(studentId, classId);
    }

    public List<StuClass> getClassStudents(Long classId) {
        return stuClassRepository.findByClassId(classId);
    }
}